package com.bmf.module.testQuestions.controller;


import com.bmf.constant.AdminResultEnum;
import com.bmf.module.testQuestions.entity.QuestionType;
import com.bmf.module.testQuestions.service.QuestionTypeService;
import com.bmf.module.testQuestions.vo.QuestionTypeCreateVo;
import com.bmf.module.testQuestions.vo.TypeResultVo;
import com.bmf.module.testQuestions.vo.TypeUpdateVo;
import com.bmf.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.bmf.core.result.R;

/**
 * (QuestionType)表控制层
 *
 * @author makejava
 * @since 2022-11-30 14:20:47
 */
@RestController
@RequestMapping("questionType")
@Api(tags = {"试题分类接口"})
public class QuestionTypeController{
    /**
     * 服务对象
     */
    @Autowired
    private QuestionTypeService questionTypeService;

    /**
     * 查询
     *
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation(value = "查询分类")
    public R<List<TypeResultVo>> selectAll() {
        return R.success(questionTypeService.selectAll());
    }

    /**
     * 新增分类
     *
     * @param questionTypeCreateVo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public R<Integer> insert(@RequestBody QuestionTypeCreateVo questionTypeCreateVo) {
        return R.success(questionTypeService.insert(questionTypeCreateVo));
    }

    /**
     * 修改分类
     *
     * @param typeUpdateVo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改分类")
    public R update(@RequestBody TypeUpdateVo typeUpdateVo) {
        boolean b = questionTypeService.updateById(BeanCopyUtils.copyBean(typeUpdateVo, QuestionType.class));
        AdminResultEnum.UPDATE_EXCEPTION.assertIsTrue(b);
        return R.ok();
    }

    /**
     * 删除分类
     *
     * @return 删除结果
     */
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除分类")
    public R<Void> delete(@PathVariable("id") Integer id) {
        boolean b = questionTypeService.removeById(id);
        AdminResultEnum.DELETE_EXCEPTION.assertIsTrue(b);
        return R.ok();
    }
}

